package server;

import java.rmi.*;   
import java.rmi.server.*;  
import java.sql.ResultSet;
import java.sql.SQLException;

public class Person extends UnicastRemoteObject implements PersonInterface{
	private Data data;
	public Person() throws RemoteException{
		data=new Data();
		
	}
	
	public String getName(int id) throws RemoteException{
		
		String sql;
		ResultSet rs=null;
		String name=null;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				name=rs.getString("name");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return name;
		
		
		}
	public String getSex(int id) throws RemoteException{
		String sql;
		ResultSet rs=null;
		String sex=null;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				sex=rs.getString("sex");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return sex;
		
		
		}
	public double getCash(int id) throws RemoteException{
		String sql;
		ResultSet rs=null;
		int cash=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				cash=rs.getInt("cash");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return cash;
		
		
		}
	public String getJob(int id) throws RemoteException{
		
		String sql;
		ResultSet rs=null;
		String job=null;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				job=rs.getString("Job");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return job;
		}
	public int getComID(int id) throws RemoteException{
		
		String sql;
		ResultSet rs=null;
		int comID=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				comID=rs.getInt("comId");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return comID;
	}
	public boolean setComID(int id,int comID)throws RemoteException{
		String sql;
		int re=0;
		
		sql="update person set comId = "+comID+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
		}
	

	public int getHp(int id) throws RemoteException{
		String sql;
		ResultSet rs=null;
		int hp=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				hp=rs.getInt("hp");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return hp;
		}
	
	public int getHap(int id) throws RemoteException{
		String sql;
		ResultSet rs=null;
		int hap=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				hap=rs.getInt("hap");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return hap;
	}
	public int getExp(int id) throws RemoteException{
		String sql;
		ResultSet rs=null;
		int exp=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				exp=rs.getInt("exp");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return exp;
	}
	
	public boolean setExp(int id,int cExp)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update person set exp="+cExp;
		re=data.update(sql);
		
		if(re==1)
		{
			return true;
		}
		return false;
	}
	
	
	public int getBankAccountID(int id) throws RemoteException{
		
		String sql;
		ResultSet rs=null;
		int bankAccountID=0;
		
		sql="select * from person where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				bankAccountID=rs.getInt("bankAccountId");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return bankAccountID;
	}
	public boolean setBankAccountID(int id,int bankAccountID) throws RemoteException{
		String sql;
		int re=0;
		
		sql="update person set bankAccountId = "+bankAccountID+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
		}
	
	public double getBankBalance(int id) throws RemoteException{return 1;}
	
	
	public boolean cashChanges(int id,double cCash,char a) throws RemoteException{ 
		String sql;
		int re=0;
		double c=0;
		if(a=='+'){
			c=cCash+getCash(id);
		}else if(getCash(id)>=cCash){
			c=getCash(id)-cCash;
		}else{
			return false;
		}
			
		sql="update person set cash = "+c+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;	
		
	}
	public boolean apply(int id,String cJob) throws RemoteException{
		String sql;
		int re=0;
		
		sql="update person set Job = '"+cJob+"' where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;	
	
	}

	public boolean hapChanges(int id,int cHap,char a) throws RemoteException{
		String sql;
		int re=0;
		double c=0;
		if(a=='+')
		{
			c=cHap+getHap(id);	
		}
		else if(getHap(id)>=cHap)
		{
			c=getHap(id)-cHap;
		}
		else {
			return false;
		}
		sql="update person set hap = "+c+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;	
		}
	
	public boolean hpChanges(int id,int cHp,char a) throws RemoteException{

		String sql;
		int re=0;
		double c=0;
		if(a=='+')
		{
			c=cHp+getHp(id);	
		}
		else if(getHp(id)>=cHp)
		{
			c=getHp(id)-cHp;
		}
		else {
			return false ;
		}
			
		sql="update person set hp = "+c+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;	
		}
	
	public boolean expChanges(int id,int cExp,char a) throws RemoteException{

		String sql;
		int re=0;
		double c=0;
		if(a=='+')
		{
			c=cExp+getExp(id);	
		}
		else if(getExp(id)>=cExp)
		{
			c=getExp(id)-cExp;
		}
		else {
			return false ;
		}
			
		sql="update person set exp = "+c+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;	
		}

	public int loginConfirm(String username,String password) throws RemoteException{
		String sql;
		ResultSet rs=null;
		int pid=-1;
		
		sql="select * from person where username='" + username +"' and password ='"+password+"'";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				pid=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return pid;
	}
	
	public boolean hasName(String username) throws RemoteException{
		String sql;
		ResultSet rs=null;
		
		sql="select * from person where username='" + username+"'";
		rs=data.getResultSet(sql);
		
		try {
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;
	}

	public int reg(String username,String password,String name,String sex) throws RemoteException{
		String sql;
		String sql2;
		ResultSet rs=null;
		int re=0;
		int pid=-1;
		
		sql="insert into person values (null,'"+username+"','"+password+"','"+name+"','"+sex+"',2000,0,90,90,0,0,'无')";
		sql2="select * from person where username='"+username+"' limit 1";
		re=data.update(sql);
		
		if(re==1){
			rs=data.getResultSet(sql2);
			try {
				if(rs.next()){
					pid=rs.getInt("id");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return pid;	
	}
	
	public int getSize(){
		String sql;
		ResultSet rs=null;
		int sum=0;
		
		sql="select count(*) as sum from person";
		rs=data.getResultSet(sql);
		
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return sum;
	}



}